//
//  ProfileViewController.swift
//  SkylowAPP
//
//  Created by 王王凤丽 on 2024/9/20.
//

import UIKit
import SnapKit
import JXSegmentedView
import JXPagingView
import zhPopupController
import Toaster

class ProfileViewController: UIViewController {
 

    private lazy var bgView = {
        let bg = UIView()
        bg.layer.cornerRadius = 12
        bg.backgroundColor = .black
        self.view.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.leading.equalTo(self.view.safeAreaLayoutGuide.snp.leading).offset(16)
            make.trailing.equalTo(self.view.safeAreaLayoutGuide.snp.trailing).offset(-16)
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(16)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-16)
        }
        return bg
    }()
    private lazy var headerView = {
        let header = ProfileHeaderView.loadFromXib()!
        header.layer.cornerRadius = 12
        header.clipsToBounds = true
        header.backgroundColor = .black
        self.view.addSubview(header)
        header.snp.makeConstraints { make in
            make.leading.equalTo(self.view.safeAreaLayoutGuide.snp.leading).offset(16)
            make.trailing.equalTo(self.view.safeAreaLayoutGuide.snp.trailing).offset(-16)
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(16)
        }
        return header
    }()
    
    private lazy var dataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = ["Character", "🔒Voices", "🔒Worlds", "🔒Personas"];
        dataSource.titleNormalColor = .white.withAlphaComponent(0.8)
        dataSource.titleSelectedColor = .white
        dataSource.titleNormalFont = .regular(12)
        dataSource.titleSelectedFont = .regular(12)
        dataSource.isTitleStrokeWidthEnabled = true
        dataSource.isTitleColorGradientEnabled = true
        return dataSource
    }()
    
    private lazy var segement = {
        let seg = JXSegmentedView()
        seg.delegate = self
        seg.dataSource = self.dataSource
        self.view.addSubview(seg)
        seg.snp.makeConstraints { make in
            make.top.equalTo(self.headerView.snp.bottom).offset(20)
            make.height.equalTo(30)
            make.leading.trailing.equalToSuperview()
        }
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorHeight = 2
        indicator.indicatorColor = .colorWith(hexString: "#B24592")
        seg.indicators = [indicator]
        
        return seg
    }()
    
    private lazy var listContainerView: JXSegmentedListContainerView! = {
        let container = JXSegmentedListContainerView(dataSource: self)
        container.layer.cornerRadius = 12
        container.clipsToBounds = true
        self.segement.listContainer = container
        self.view.addSubview(container)
        container.snp.makeConstraints { make in
            make.leading.equalTo(bgView.snp.leading)
            make.trailing.equalTo(bgView.snp.trailing)
            make.top.equalTo(self.segement.snp.bottom)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-16)
        }
        return container
    }()
    
    private lazy var editPop: zhPopupController = {
        let editPop =  zhPopupController.init(view: self.editProfileView, size: CGSizeMake(kScreenWidth, 480))
        editPop.presentationStyle = .fromBottom
        editPop.layoutType = .bottom
        return editPop
    }()
    
    private lazy var editProfileView: SLEditProfilePopView = {
        let editProfileView =  SLEditProfilePopView.loadFromXib()!
        editProfileView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 480)
        editProfileView.addCorner(conrners: [.topLeft,.topRight], radius: 12)
        
        editProfileView.cancelBlock = { [unowned self] in
            self.editPop.dismiss()
        }
        return editProfileView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .init(51, 51, 51)
        
        configNav()
        _ = bgView
        _ = headerView
        _ = segement
        _ = listContainerView
        
        
        headerView.editProfileBlock = { [unowned self] in
            self.editPop.show(in: self.view, duration: 0.25)
        }
    }
    
}

extension ProfileViewController {
    func configNav() {
        
        navigationItem.title = "Skylow"
        let search = UIBarButtonItem(image: UIImage(named: "profile_search_icon")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(searchAction))
        navigationItem.leftBarButtonItem = search
        
        let moreItem = UIBarButtonItem(image: UIImage(named: "profile_more_icon")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(moreAction))
        navigationItem.rightBarButtonItem = moreItem
    }
    @objc func searchAction() {
        
    }
    
    @objc func moreAction() {
        
    }
    
}

extension ProfileViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        dataSource.titles.count
    }
    
    
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> any JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            return SkylowProfileCharaterViewController()
        }
        
        if index == 1 {
            return SkylowProfileVoicesViewController()
        }
        
        if index == 2 {
            return SkylowProfileWorldsViewController()
        }
        
        if index == 3 {
            return SkylowProfilePersonasViewController()
        }
      
        assert(false, "没有对应选项...")
    }
}


extension ProfileViewController: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {
        
    }
    
  
    func segmentedView(_ segmentedView: JXSegmentedView, canClickItemAt index: Int) -> Bool {
        if index != 0 {
            
            EWToast.showCenterWithText(text: "Coming soon", duration: 2)

            return false
        }
        return true
    }
    
}
